import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

import 'public_alert_dialog.dart';

/// FileName will_pop_dialog
///
/// @Author lax
/// @Date 2023/6/2
///
/// @Description

class WillPopDialog extends StatelessWidget {
  final BuildContext context;

  /// 返回上一页是否需要弹窗提示 默认true
  final bool showAlertWillPop;

  /// 弹窗提示内容
  final String? title;

  /// 点击确认按钮 默认执行pop
  final VoidCallback? onConfirm;

  final Widget? child;

  const WillPopDialog({
    super.key,
    required this.context,
    this.showAlertWillPop = true,
    this.title,
    this.onConfirm,
    this.child,
  });

  @override
  Widget build(BuildContext context) {
    if (kIsWeb || !Platform.isAndroid || !showAlertWillPop) {
      return child ?? const SizedBox();
    }
    return WillPopScope(
      onWillPop: () async {
        PublicAlertDialog.show(
          context: context,
          title: title ?? '',
          onConfirm: () async {
            if (onConfirm != null) {
              onConfirm!();
            } else {
              Navigator.pop(context);
            }
          },
        );
        return false;
      },
      child: child ?? const SizedBox(),
    );
  }
}
